package com.orders.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.orders.pojo.Customer;
import com.orders.pojo.Goods;

public class GoodsOperation implements Operation {
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	private Goods goods;
	@Override
	public boolean insert(Object obj) {
		try {
			if(obj instanceof Goods){
				goods = (Goods)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("insert into goods values (?,?,?,?,?,?,?,?,?,?)");
				ps.setInt(1, goods.getId());
				ps.setInt(2, goods.getGid());
				ps.setString(3, goods.getTitle());
				ps.setString(4, goods.getDescription());
				ps.setDouble(5, goods.getPrice());
				ps.setInt(6, goods.getStock());
				ps.setString(7, goods.getStatus());
				ps.setInt(8, goods.getUser().getId());
				ps.setInt(9, goods.getClassify().getId());
				ps.setBoolean(10, goods.isVisible());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return false;
	}

	@Override
	public boolean update(Object obj) {
		try {
			if(obj instanceof Goods){
				goods = (Goods)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update goods set gid = ?,title = ?,description = ?,price = ?,stock = ?,status = ?,user_id = ?,classify_id = ? visible = ? where id = ?");
				ps.setInt(1, goods.getGid());
				ps.setString(2, goods.getTitle());
				ps.setString(3, goods.getDescription());
				ps.setDouble(4, goods.getPrice());
				ps.setInt(5, goods.getStock());
				ps.setString(6, goods.getStatus());
				ps.setInt(7, goods.getUser().getId());
				ps.setInt(8, goods.getClassify().getId());
				ps.setBoolean(9, goods.isVisible());
				ps.setInt(10, goods.getId());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return false;
	}

	@Override
	public List select(Object obj) {
		UserOperation uo = new UserOperation();
		List list = new ArrayList<>();
		try {
			if(obj instanceof Goods){
				goods = (Goods)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from goods where id = ? and visible = 1");
				ps.setInt(1, goods.getId());
				rs = ps.executeQuery();
				Goods goods = new Goods();
				while(rs.next()){
					goods.setId(rs.getInt("id"));
					goods.setGid(rs.getInt("gid"));
					goods.setPrice(rs.getDouble("price"));
					goods.setClassify(null);
					goods.setDescription(rs.getString("description"));
					goods.setRole(null);
					goods.setStock(rs.getInt("stock"));
					goods.setStatus("status");
					goods.setTitle(rs.getString("title"));
					goods.setUser(uo.selectById(rs.getInt("user_id")));
					list.add(goods);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return list;
	}

	public Goods selectByGid(String gid) {
		UserOperation uo = new UserOperation();
		goods = new Goods();
		try {
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from goods where gid = ? and visible = 1");
				ps.setString(1, gid);
				rs = ps.executeQuery();
				Goods goods = new Goods();
				rs.next();
					goods.setId(rs.getInt("id"));
					goods.setGid(rs.getInt("gid"));
					goods.setPrice(rs.getDouble("price"));
					goods.setClassify(null);
					goods.setDescription(rs.getString("description"));
					goods.setRole(null);
					goods.setStock(rs.getInt("stock"));
					goods.setStatus("status");
					goods.setTitle(rs.getString("title"));
					goods.setUser(uo.selectById(rs.getInt("user_id")));
				
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return goods;
	}
	
	public Goods selectById(int id) {
		UserOperation uo = new UserOperation();
		goods = new Goods();
		try {
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from goods where gid = ? and visible = 1");
				ps.setInt(1, id);
				rs = ps.executeQuery();
				Goods goods = new Goods();
				rs.next();
					goods.setId(rs.getInt("id"));
					goods.setGid(rs.getInt("gid"));
					goods.setPrice(rs.getDouble("price"));
					goods.setClassify(null);
					goods.setDescription(rs.getString("description"));
					goods.setRole(null);
					goods.setStock(rs.getInt("stock"));
					goods.setStatus("status");
					goods.setTitle(rs.getString("title"));
					goods.setUser(uo.selectById(rs.getInt("user_id")));
				
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return goods;
	}
	
	@Override
	public boolean delete(Object obj) {
		try {
			if(obj instanceof Goods){
				goods = (Goods)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update goods set visible = ? where id = ?");
			    ps.setBoolean(1, false);
			    ps.setInt(2, goods.getId());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return false;
	}

}
